﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Text;
using System.IO;
namespace DataProvider
{
    class EntitySqlConfig
    {
        private String entityName = "";

        public String EntityName
        {
            get { return entityName; }
            set { entityName = value; }
        }
        private String insertSql = "";

        public String InsertSql
        {
            get { return insertSql; }
            set { insertSql = value; }
        }
        private String updateSql = "";

        public String UpdateSql
        {
            get { return updateSql; }
            set { updateSql = value; }
        }
        private String deleteSql = "";

        public String DeleteSql
        {
            get { return deleteSql; }
            set { deleteSql = value; }
        }

        public void LoadFromXml(XmlReader reader)
        {
            reader.MoveToContent();
            do
            {
                if (String.Compare("entity", reader.Name, true) == 0)
                {
                    if (String.IsNullOrEmpty(reader.GetAttribute("name")))
                        continue;
                    this.entityName = reader.GetAttribute("name");
                }
                else if (String.Compare("insert", reader.Name, true) == 0)
                {
                    this.insertSql = reader.ReadString().Trim().TrimStart('\r', '\n').TrimStart('\r', '\n');
                }
                else if (String.Compare("update", reader.Name, true) == 0)
                {
                    this.updateSql = reader.ReadString().Trim().TrimStart('\r', '\n').TrimStart('\r', '\n');
                }
                else if (String.Compare("delete", reader.Name, true) == 0)
                {
                    this.deleteSql = reader.ReadString().Trim().TrimStart('\r', '\n').TrimStart('\r', '\n');
                }
            } while (reader.Read());
        }

        public String ToXml()
        {
            return String.Format(@" <!-- {4} -->
                                    <entity name={0}>
                                    <insert>
                                      <![CDATA[{1}]]>
                                    </insert>
                                    <update>
                                      <![CDATA[{2}]]>
                                    </update>
                                    <delete>
                                      <![CDATA[{3}]]>
                                    </delete>
                                  </entity>
                                   ", '"' + this.entityName + '"', this.insertSql, this.updateSql, this.deleteSql, this.entityName);
        }
    }

    class SqlConfigFile
    {
        public Dictionary<String, EntitySqlConfig> EntitySqlConfigCollection = new Dictionary<string, EntitySqlConfig>(StringComparer.OrdinalIgnoreCase);
        public void LoadFile(String fileName)
        {
            using (StreamReader fileReader = new StreamReader(fileName, Encoding.Default))
            {
                String xml = fileReader.ReadToEnd();
                LoadXml(xml);
            }
        }
        public void LoadXml(String xml)
        {
            using (XmlReader reader = new XmlTextReader(xml, XmlNodeType.Element, null))
            {
                do
                {
                    if (String.Compare("entity", reader.Name, true) == 0)
                    {
                        EntitySqlConfig configItem = new EntitySqlConfig();
                        configItem.LoadFromXml(reader.ReadSubtree());
                        EntitySqlConfigCollection[configItem.EntityName] = configItem;
                    }
                } while (reader.Read());
            }
        }

        public void SaveFile(String fileName)
        {
            StringBuilder xml = new StringBuilder();
            xml.Append("<entitySqlSet>").AppendLine();
            if (!EntitySqlConfigCollection.ContainsKey("Sample"))
            {
                xml.Append(@"<entity name=" + '"' + "Sample" + '"' + @">
                            <insert>
                              <![CDATA[
                                DECLARE @EID varchar(50)
                               
                                Select @EID=ID    
                                From Sample
                                Where ID =@ID
                        	   
                                IF @@ROWCOUNT >0
                                BEGIN
                                  Select @EID
                                END
                                ELSE
                                BEGIN
                                 Insert into Sample(XX,BBB,EEE)
                                 SELECT @XX,@BBB,@EEE
                                 
                                 SELECT @ID
                                END
                               ]]>
                            </insert>
                            <update>
                              <![CDATA[]]>
                            </update>
                            <delete>
                              <![CDATA[]]>
                            </delete>
                         </entity>");
            }
            foreach (KeyValuePair<String, EntitySqlConfig> configItem in EntitySqlConfigCollection)
            {
                xml.Append(configItem.Value.ToXml()).AppendLine();
            }
            xml.Append("  </entitySqlSet>").AppendLine().AppendLine();
            using (StreamWriter writer = new StreamWriter(fileName, false, Encoding.Default))
            {
                writer.Write(xml.ToString());
            }
        }
    }
}
